/*
 * Copyright 2010 Piotr Buda
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package li.buda.cqflex.commandbus {
import flash.utils.Dictionary;
import flash.utils.getQualifiedClassName;

import li.buda.cqflex.logging.LogFactory;
import li.buda.cqflex.validate.Validate;

import mx.logging.ILogger;

/**
 * The <code>SimpleCommandBus</code> is an implementation of the <code>CommandBus</code>.
 * It uses a <code>Dictionary</code> with weak keys to store command to command handler mappings.
 * This implementation allows for only one command handler to be subscribed to the bus
 * for a particular command type.
 * @see li.buda.cqflex.commandbus.Command
 * @see li.buda.cqflex.commandbus.CommandHandler
 * @see li.buda.cqflex.commandbus.CommandBus
 * @author Piotr Buda
 * @since 0.1
 */
public class SimpleCommandBus implements CommandBus {
    private static var log:ILogger = LogFactory.getLogger(SimpleCommandBus);
    private var handlers:Dictionary;

    public function SimpleCommandBus() {
        handlers = new Dictionary(true);
    }

    /**
     * @inheritDoc
     */
    public function publish(command:Command):void {
        Validate.notNull(command);
        //Find the command handler, if any, for this command
        log.debug("Attempting to find command handler");
        var h:CommandHandler = handlers[getQualifiedClassName(command)];
        if (h) { //Command handler for this command is found
            //Execute the command handler
            log.debug("Executing command handler");
            h.execute(command);
            return;
        }
        //There is no command handler for this command, raise an error
        throw new NoCommandHandlerError(command);
    }

    /**
     * @inheritDoc
     */
    public function subscribe(commandType:Class, commandHandler:CommandHandler):void {
        Validate.notNull(commandType);
        Validate.notNull(commandHandler);
        handlers[getQualifiedClassName(commandType)] = commandHandler;
    }

    /**
     * The <code>SimpleCommandBus</code> allows only one handler be subscribed for one
     * <code>commandType</code> thus the <code>commandHandler</code> argument is optional.
     * @inheritDoc
     */
    public function unsubscribe(commandType:Class, commandHandler:CommandHandler = null):void {
        Validate.notNull(commandType);
        var cmdType:String = getQualifiedClassName(commandType);
        if (handlers[cmdType]) {
            delete handlers[cmdType];
            return;
        }
        //There is no such mapping, raise an error
        throw new NoCommandHandlerError(commandType);
    }
}
}